package baseTest;

import java.util.ArrayList;
import java.util.List;

public class backtrack {
    public static void main(String[] args) {
        int[] arr = {2,3,6,7};
        int target = 7;
        List<List<Integer>> process = process(arr, target);
        for (List<Integer> list:process) {
            System.out.println(list);
        }
    }
    public static List<List<Integer>>process(int[] candidates,int target){
        if (candidates.length == 0 || candidates == null){
            return null;
        }
        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        process(lists,list,candidates,target,0);
        return lists;
    }

    public static void process(List<List<Integer>> lists,List<Integer> list,int[] candidates,int target,int cur){
        if (target == 0){
            lists.add(new ArrayList<>(list));
            return;
        }
        for (int i = cur; i < candidates.length; i++) {
            if (target- candidates[i] < 0){
                break;
            }
            list.add(candidates[i]);
            process(lists,list,candidates,target-candidates[i],i);
            list.remove(list.size()-1);
        }
    }
}
